package com.jiayuan.cn.energy.service;

import com.jiayuan.cn.energy.util.RSACoder;
import com.jiayuan.cn.energy.util.RSAUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

@Component
@Slf4j
public class UserService {
//    private static final RSAUtil.RsaKeyPair rsaKeyPair;
    private static final Map<String, Key> mapKeyPair;

    static {
        try {
//            rsaKeyPair = RSAUtil.generateKeyPair();
            mapKeyPair = RSACoder.initKey();
        } catch (NoSuchAlgorithmException e) {
            log.error("Error generating:{}", e.getMessage());
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public Key getKeyPublic()  {
        return mapKeyPair.get(RSACoder.PUBLIC_KEY);
    }
    public Key getKeyPrivate()  {
        return mapKeyPair.get(RSACoder.PRIVATE_KEY);
    }

    public String getPublicKey()  {
//        return rsaKeyPair.getPublicKey();
//        return mapKeyPair.get(RSACoder.PUBLIC_KEY).toString();
        try {
            return RSACoder.getPublicKey(mapKeyPair);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String getPrivateKey()  {
//        return rsaKeyPair.getPrivateKey();
//        return mapKeyPair.get(RSACoder.PRIVATE_KEY).toString();
        try {
            return RSACoder.getPrivateKey(mapKeyPair);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
